package com.lepu.app.fun.my;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.application.MyApplication;
import com.app.config.UserConfig;
import com.app.utils.Const;
import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.app.utils.UpdateManager;
import com.app.utils.Utils;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.CallBack;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.widget.MyDialog;
import com.lepu.app.main.activity.AppBroswerActivity;
import com.lepu.app.usercenter.ModifyPwdActivity;
import com.lepu.app.usercenter.ModifyPwdQuestionActivity;
import com.lepu.app.usercenter.bean.LoginInfo;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.pasm.R;

public class MySettingActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener {

	private static final int MSG_CLEAN_CACHE = 10;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_setting_activity);

		init();
	}

	@Override
	public void onResume() {
		super.onResume();

		updateLoginState();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("个人设置");
		topbar.setonTopbarNewLeftLayoutListener(this);

		RelativeLayout mobileLayout = (RelativeLayout) findViewById(R.id.mobileLayout);
		RelativeLayout modifyPwdLayout = (RelativeLayout) findViewById(R.id.modifyPwdLayout);
		RelativeLayout pwdQuestionLayout = (RelativeLayout) findViewById(R.id.pwdQuestionLayout);
		RelativeLayout aboutLayout = (RelativeLayout) findViewById(R.id.aboutLayout);
		RelativeLayout cleanCacheLayout = (RelativeLayout) findViewById(R.id.cleanCacheLayout);
		RelativeLayout versionLayout = (RelativeLayout) findViewById(R.id.versionLayout);
		RelativeLayout protolLayout = (RelativeLayout) findViewById(R.id.protolLayout);
		Button loginStateButton = (Button) findViewById(R.id.loginStateButton);

		mobileLayout.setOnClickListener(mOnClickListener);
		pwdQuestionLayout.setOnClickListener(mOnClickListener);
		modifyPwdLayout.setOnClickListener(mOnClickListener);
		aboutLayout.setOnClickListener(mOnClickListener);
		cleanCacheLayout.setOnClickListener(mOnClickListener);
		versionLayout.setOnClickListener(mOnClickListener);
		protolLayout.setOnClickListener(mOnClickListener);
		loginStateButton.setOnClickListener(mOnClickListener);

		// update version info
		String version = "";
		try {
			version = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

		String debug = "";
		if (Setting.DEBUG) {
			debug = " beta ";
		}

		TextView versionTextView = (TextView) findViewById(R.id.versionTextView);
		versionTextView.setText(debug + version);

		//声音开关
		ImageView chatAudioSwitchImageView = (ImageView) findViewById(R.id.chatAudioSwitchImageView);
		chatAudioSwitchImageView.setOnClickListener(mOnClickListener);
		boolean flag = UserConfig.getConfigBoolean(this, Const.CONFIG_APP_SETTING_CHAT_AUDIO_SWITCH,false);
		chatAudioSwitchImageView.setSelected(flag);
	}

	private void updateLoginState() {
		TextView pwdQuestionStateTextView = (TextView) findViewById(R.id.pwdQuestionStateTextView);
		Button loginStateButton = (Button) findViewById(R.id.loginStateButton);

		if (LoginUtil.checkIsLogin()) {
			loginStateButton.setTag(0);
			loginStateButton.setText("退出登录");

			LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
			if (!TextUtils.isEmpty(loginInfo.HasSecurityQuestion)
					&& loginInfo.HasSecurityQuestion.equals("1")) {
				pwdQuestionStateTextView.setText("修改");
			} else {
				pwdQuestionStateTextView.setText("未设置");
			}
		} else {
			loginStateButton.setTag(1);
			loginStateButton.setText("去登录");

			pwdQuestionStateTextView.setText("");
		}
	}

	private View.OnClickListener mOnClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {

			Intent intent = null;
			switch (v.getId()) {
			case R.id.mobileLayout:

				boolean flag = LoginUtil.jumpLogin(MySettingActivity.this);

				if (!flag) {
					intent = new Intent(MySettingActivity.this,
							MyMobileActivity.class);
					startActivity(intent, true);
				}
				break;
			case R.id.pwdQuestionLayout:

				boolean flag1 = LoginUtil.jumpLogin(MySettingActivity.this);

				if (!flag1) {
					intent = new Intent(MySettingActivity.this,
							ModifyPwdQuestionActivity.class);
					startActivity(intent, true);
				}

				break;
			case R.id.modifyPwdLayout:

				boolean flag2 = LoginUtil.jumpLogin(MySettingActivity.this);
				if (!flag2) {
					intent = new Intent(MySettingActivity.this,
							ModifyPwdActivity.class);
					startActivity(intent, true);
				}

				break;
			case R.id.aboutLayout:
				intent = new Intent(MySettingActivity.this,
						AppBroswerActivity.class);
				intent.putExtra("title", "关于我们");
				intent.putExtra("no_need_menu", true);
				intent.putExtra("url",
						"file:///android_asset/about_us/about_us.html");
				startActivity(intent, true);
				break;
			case R.id.versionLayout:

				UpdateManager.getInstance().checkUpdate(false,
						Setting.getCheckUpdate(), MySettingActivity.this, true,
						true);
				break;
			case R.id.protolLayout:
				intent = new Intent(MySettingActivity.this,
						AppBroswerActivity.class);
				intent.putExtra("title", "免责声明");
				intent.putExtra("no_need_menu", true);
				intent.putExtra("url",
						"file:///android_asset/protol/protol.html");
				startActivity(intent, true);
				break;
			case R.id.cleanCacheLayout:

				cleanCache();
				break;
			case R.id.loginStateButton:

				int tag = (Integer) v.getTag();
				if (tag == 0) {
					new MyDialog(MySettingActivity.this)
							.setMessage("确定退出当前账户?")
							.setNegativeButton(R.string.app_cancel,
									new View.OnClickListener() {

										@Override
										public void onClick(View v) {

										}
									})
							.setPositiveButton(R.string.app_quit,
									new View.OnClickListener() {

										@Override
										public void onClick(View v) {
											// log out
											LoginUtil.loginOut();
											updateLoginState();
										}
									}).create(null).show();

				} else {
					// login
					LoginUtil.jumpLogin(MySettingActivity.this);
				}
				break;
				case R.id.chatAudioSwitchImageView:

					boolean flagChat = !v.isSelected();
					v.setSelected(flagChat);

					UserConfig.setConfig(MySettingActivity.this, Const.CONFIG_APP_SETTING_CHAT_AUDIO_SWITCH, flagChat);
					break;

			default:
				break;
			}
		}
	};

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	@SuppressLint("HandlerLeak")
	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case MSG_CLEAN_CACHE:

				hideProgressDialog();
				UIHelper.showToast(MySettingActivity.this, "清理缓存成功");

				break;

			default:
				break;
			}
		}
	};

	private void cleanCache() {
		new MyDialog(this)
				.setMessage("您确定清理缓存吗?")
				.setNegativeButton(R.string.app_cancel,
						new View.OnClickListener() {

							@Override
							public void onClick(View v) {

							}
						})
				.setPositiveButton(R.string.app_ok, new View.OnClickListener() {

					@Override
					public void onClick(View v) {

						showProgressDialog();
						// 清除缓存文件
						Utils.cleanAllCache(MySettingActivity.this,
								new CallBack() {
									public void callBackSuccess(Object obj) {
										mHandler.sendEmptyMessage(MSG_CLEAN_CACHE);
									}
								});
					}
				}).create(null).show();
	}

}
